<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="商户名称：" prop="clientName">
          <el-input v-model="queryParams.clientName" placeholder="请输入" clearable style="width: 260px;" />
        </el-form-item>
        <el-form-item label="服务状态：" prop="serviceStatus">
          <el-select v-model="queryParams.serviceStatus" placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in serviceList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户渠道：" prop="source">
          <el-select v-model="queryParams.source" placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户负责人：" prop="personIds">
          <el-select v-model="queryParams.personIds" multiple placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in responsiblePersonList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="查询时间：">
          <el-date-picker clearable v-model="date" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime" placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head" style="margin: 0 0 20px">
        <div>
          <el-button type="primary" @click="salesUpLoad">售卖明细导入</el-button>
          <el-button type="primary" @click="writeOffUpLoad">核销记录导入</el-button>
        </div>
        <export-excel-button title="销售数据总汇" action="/service/summary/v1/export" :params="queryParams" v-hasAuth="'client:service:summary:export'"/>
      </div>
      <el-table v-loading="loading" ref="multipleTable" :data="tabList" stripe border tooltip-effect="dark"
        style="width: 100%">
        <el-table-column label="商户信息" align="center">
          <el-table-column label="序号" align="center" type="index" width="64px" />
          <el-table-column label="客户名称" align="center" prop="clientName"  width="100px"></el-table-column>
          <el-table-column label="服务时间" align="center" prop="serviceTime" width="100px"></el-table-column>
          <el-table-column label="服务状态" align="center" prop="serviceStatus"></el-table-column>
          <el-table-column label="服务类型" align="center" prop="serviceType"  width="100px"></el-table-column>
          <el-table-column label="客户渠道" align="center" prop="source"></el-table-column>
          <el-table-column label="渠道名称/其他备注" align="center" prop="sourceNameStr"></el-table-column>
          <el-table-column label="前置费" align="center" prop="frontMoney"></el-table-column>
          <el-table-column label="前置费收取时间" align="center" prop="frontMoneyTime"  width="100px"></el-table-column>
          <el-table-column label="客户负责人" align="center" prop="responsiblePerson"></el-table-column>
          <el-table-column label="抖音林客账号" align="center" prop="tiktokLinkeStatus"></el-table-column>
          <el-table-column label="服务佣金" align="center" prop="serviceCommission"></el-table-column>
          <el-table-column label="客户备注" align="center" prop="remark" width="100px">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top" width="300" >
                <p>{{ scope.row.remark }}</p>
                <div slot="reference">
                  <span class="ellipsis">{{ scope.row.remark }}</span>
                </div>
              </el-popover>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="抖音直播订单" align="center">
          <el-table-column label="订单量" align="center" prop="liveOrderNum"></el-table-column>
          <el-table-column label="销售额" align="center" prop="liveSales"></el-table-column>
          <el-table-column label="已核销(金额)" align="center" prop="liveVerify"></el-table-column>
          <el-table-column label="未核销(金额)" align="center" prop="liveUnVerify"></el-table-column>
          <el-table-column label="退款订单量" align="center" prop="liveRefundOrderNum"></el-table-column>
          <el-table-column label="退款金额" align="center" prop="liveRefund"></el-table-column>
        </el-table-column>
        <el-table-column label="抖音团购订单" align="center">
          <el-table-column label="订单量" align="center" prop="groupOrderNum"></el-table-column>
          <el-table-column label="销售额" align="center" prop="groupSales"></el-table-column>
          <el-table-column label="已核销(金额)" align="center" prop="groupVerify"></el-table-column>
          <el-table-column label="未核销(金额)" align="center" prop="groupUnVerify"></el-table-column>
          <el-table-column label="退款订单量" align="center" prop="groupRefundOrderNum"></el-table-column>
          <el-table-column label="退款金额" align="center" prop="groupRefund"></el-table-column>
        </el-table-column>
        <el-table-column label="收款明细" align="center">
          <el-table-column label="总订单数" align="center" prop="detailOrderNum"></el-table-column>
          <el-table-column label="总销售额" align="center" prop="detailSales"></el-table-column>
          <el-table-column label="总退单数" align="center" prop="detailRefundNum"></el-table-column>
          <el-table-column label="总退款金额" align="center" prop="detailRefundTotal"></el-table-column>
          <el-table-column label="总订单核销额" align="center" prop="detailOrderVerifyTotal"></el-table-column>
          <el-table-column label="总核销额（合作时间内核销总金额）" align="center" prop="detailVerifyTotal"></el-table-column>
          <el-table-column label="佣金" align="center" prop="detailCommission"></el-table-column>
          <el-table-column label="更新时间" align="center" prop="updateTime"></el-table-column>

        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" style="width: 240px;">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="salesClick(scope.row)">售卖明细</el-button>
            <el-button type="text" size="small" @click="writeOffClick(scope.row)">核销记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList"></pagination>
    </div>

    <!-- 直播结果弹窗 -->
    <SalesDialog v-if="dialogVisible" :id="dialogId" :pathType="pathType" :visible.sync="dialogVisible"
      @success="_handleSuccess">
    </SalesDialog>
  </div>
</template>

<script>
import {
  summaryList,
} from "@/api/customer/serviceData";
import {
  responsibleSelect,
} from "@/api/customer/customerFile";
import SalesDialog from "./components/salesDialog";
import ExportExcelButton from "@/components/ExportExcelButton/index";
export default {
  name: "",
  components: {
    SalesDialog,
    ExportExcelButton
  },
  data() {
    return {
      dialogId: '',
      dialogVisible: false,
      watchVisible: false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      // 总条数
      total: 0,
      //表格数据
      tabList: [],
      pathType: 'add',
      date: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beginTime: '',
        endTime: '',
        clientName: '',
        serviceStatus: '',
        source: '',
        personIds: [],
      },
      shopType: "",
      responsiblePersonList: [],
      serviceList: [
        { id: '', value: '全部' },
      ],//服务状态
      statusList: [
        { id: '', value: '全部' },
      ],
    };
  },
  computed: {

  },
  created() {
    this.getDict()
    this.getresponsiblePersonList()
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      let data = JSON.parse(JSON.stringify(this.queryParams));  //深拷贝
      data.personIds = data.personIds.toString()
      summaryList(data).then(response => {
        this.tabList = response.result.records;
        this.tabList = this.tabList.map(item => {
          Object.keys(item).forEach(key => {
            if (item[key] === '') {
              item[key] = '-'
            }
          })
          return item
        })
        this.total = response.result.total;
        this.loading = false;
      });
    },
    //客户负责人下拉
    getresponsiblePersonList() {
      responsibleSelect().then((res) => {
        this.responsiblePersonList = res.result
      })
        .finally(() => {
          this.loading = false;
        });
    },

    //获取状态字典
    getDict() {
      var dictType = "bee_client_source,bee_client_status";
      this.$getSelectList(dictType)
        .then((res) => {
          this.statusList = this.statusList.concat(res.result[0].dictList)
          this.serviceList = this.serviceList.concat(res.result[1].dictList)
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //处理时间
    changeTime(e) {
      this.queryParams.beginTime = e[0]
      this.queryParams.endTime = e[1]
    },
    // 表单重置
    reset() {
      this.date = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        beginTime: '',
        endTime: '',
        clientName: '',
        serviceStatus: '',
        source: '',
        personIds: [],
      };
      this.resetForm("queryParams");
      this.handleQuery()
    },
    salesUpLoad() {
      this.dialogVisible = true;
      this.pathType = 'sales'
    },
    writeOffUpLoad() {
      this.dialogVisible = true;
      this.pathType = 'write'
    },
    //售卖明细
    salesClick(row) {
      this.$router.push({
        path: '/customer/serviceData/salesRecord',
        query: {
          name: row.clientName,
          id: row.clientId,
        }
      })
    },
    //核销记录
    writeOffClick(row) {
      this.$router.push({
        path: '/customer/serviceData/writeOffRecord',
        query: {
          name: row.clientName,
          id: row.clientId,
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.reset();
    },
    _handleSuccess(e) {
      //成功回调
      this.resetQuery()
    },
  }
};

</script>
<style scoped>
.head {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px 0;
}

.listBox {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px;
  margin-top: 10px;
}

.listBox_head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.img_box {
  width: 100px;
  height: 80px;
  border-radius: 4px;
}

.contentTitle {
  width: 100%;
  display: flex;
}

.ellipsis {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.el-popover {
  height: auto;
  color: #fff;
  background: rgba(0, 0, 0, 0.8);
}
</style>
